import java.awt.event.*;
import java.io.File;

import javax.swing.*;

/**
 * EBook Organizer
 * 4/30/2010
 * <P>
 * Interacts between the Settings Model and View
 * </P>
 * @version 0.1
 */
public class SettingsController {
	private SettingsModel model;
	private JFileChooser fileChooser = new JFileChooser();
	private JFileChooser folderChooser = new JFileChooser();
	
	SettingsView view;
	
	public SettingsController() {
		fileChooser.setCurrentDirectory(new java.io.File("."));
		
		folderChooser.setCurrentDirectory(new java.io.File("."));
		folderChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
		folderChooser.setAcceptAllFileFilterUsed(false);
	}
	
	private class SetPathNameListener implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent arg0) {
				String pathName = view.getFilePath().getText();
				model.setFilePathName(pathName);
		}
	}
	
	private class AddFileTypeListener implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent arg0) {
			String fileTypeString = view.getTypejta().getText();
			String pathName = view.getProgramPath().getText();
			FileType type = new FileType(fileTypeString, pathName);
			
			if ( model.containsFileType(type) ) {
				JOptionPane.showMessageDialog(null, "This file type is already created");
			}
			else
				model.addFileType(type);
		}
	}
	
	private class RemoveFileTypeListener implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent arg0) {
				model.removeFileType( (FileType)view.getFileTypes().getSelectedItem() );
		}
	}
	
	private class BrowseProgramListener implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent arg0) {
			if (fileChooser.showOpenDialog( null ) == JFileChooser.APPROVE_OPTION) {
				File file = fileChooser.getSelectedFile();
				view.getProgramPath().setText( file.getPath() );
			}
		}
	}
	
	private class BrowseFileListener implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent arg0) {
			
			if (fileChooser.showOpenDialog( null ) == JFileChooser.APPROVE_OPTION) {
				File file = fileChooser.getSelectedFile();
				view.getFilePath().setText( file.getPath() );
			}
		}
	}
	
	public void setModel( SettingsModel m) {
		model = m;
	}
	
	public SettingsModel getModel() {
		return model;
	}
	
	public void setSettingsView(SettingsView v) {
		view = v;
		
		ActionListener setPathNameListener = new SetPathNameListener();
		view.getSetFilePathButton().addActionListener( setPathNameListener );
		
		ActionListener addFileTypeListener = new AddFileTypeListener();
		view.getAddFileTypeButton().addActionListener( addFileTypeListener );
		
		ActionListener removeFileTypeListener = new RemoveFileTypeListener();
		view.getRemoveFileTypeButton().addActionListener( removeFileTypeListener );
		
		ActionListener browseProgramListener = new BrowseProgramListener();
		view.getBrowseButton().addActionListener( browseProgramListener );
		
		ActionListener browseFileListener = new BrowseFileListener ();
		view.getFileBrowseButton().addActionListener( browseFileListener );
		
	}
	
}
